package com.example.mybatis;

import com.example.mybatis.io.Resources;
import com.example.mybatis.pojo.User;
import com.example.mybatis.sqlsession.SqlSession;
import com.example.mybatis.sqlsession.SqlSessionFactory;
import com.example.mybatis.sqlsession.SqlSessionFactoryBuilder;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;


/**
 * @Classname TestMyFm
 * @Date 2020/12/20 22:41
 * @@author admin
 * @Description TODO
 */
public class TestMyFm {


    @Test
    public void test() throws Exception {
        InputStream resourceAsSteam = Resources.getResourceAsSteam("SqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsSteam);
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //调用
        User user = new User();
        user.setId(1);
        user.setUsername("张三");

        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
//
//        List<User> all = userMapper.findAll();
//        for (User user1 : all) {
//            System.out.println(user1);
//        }

        //insert
        user.setId(100);
        user.setUsername("example");
        int reault = userMapper.insert(user);
        System.out.println(">>>>>>> insert success :result = " + reault);

        User temp = userMapper.findByCondition(user);
        System.out.println(">>>>>>> find success :result = " + temp.toString());

        //update
        user.setBirthday("2020年12月20日");
        reault = userMapper.update(user);
        System.out.println(">>>>>>> update success :result = " + reault);

        temp = userMapper.findByCondition(user);
        System.out.println(">>>>>>> find success :result = " + temp.toString());

        //delete
        reault = userMapper.delete(user);
        System.out.println(">>>>>>> delete success :result = " + reault);

        temp = userMapper.findByCondition(user);
        System.out.println(">>>>>>> find success :result = " + temp);
    }
}

